package com.playtime.layouts.example1;

import javafx.animation.FadeTransition;
import javafx.animation.SequentialTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class MoveAnimation {

    private Node node;
    private Pane newParent;

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }

    public Pane getNewParent() {
        return newParent;
    }

    public void setNewParent(Pane newParent) {
        this.newParent = newParent;
    }

    public void play() {

        FadeTransition fadeOut = new FadeTransition(Duration.millis(200), node);
        fadeOut.setFromValue(1.0f);
        fadeOut.setToValue(0f);
        fadeOut.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                newParent.getChildren().add(node);
            }
        });

        FadeTransition fadeIn = new FadeTransition(Duration.millis(200), node);
        fadeIn.setFromValue(0f);
        fadeIn.setToValue(1.0f);

        SequentialTransition move = new SequentialTransition();
        move.getChildren().addAll(fadeOut, fadeIn);

        move.playFromStart();
    }
}
